package cn.shoukaiseki.sjf4j;

import cn.hutool.core.util.StrUtil;
import com.google.common.io.LineReader;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * 测试使用的,因为同时测试多个json文件,遍历起来逻辑层较多
 * 而且还需要读取后的文件close,所以直接存到map简单点
 * map中的key使用 [文件名]-[行号] 的形式,value存放行内容
 */
public class JsonFileLineReader{

    private final int size;

    private final Set<String> keySet;

    private final Iterator<String> keyIterator;

    Map<String,String> lineMap=new LinkedHashMap<>();



    private final static String JSON_PATH="example/json/";
    //json文件要测试的内容一行一个
    private final static String NCPR_JSON = "ncpr.json";

    private final static String SKS_JSON = "sks.json";

    private FileReader  ncprFileReader=null;

    private LineReader ncprReader=null;

    private FileReader  sksFileReader=null;

    private LineReader sksReader=null;

    //当前行key
    private String currentKey;
    //当前行json内容
    private String currentLine;

    public JsonFileLineReader() throws Exception {
        String line;
        try {
            URL resources = ClassLoader.getSystemClassLoader().getResource(JSON_PATH+NCPR_JSON);
            ncprFileReader = new FileReader(resources.getFile());
            ncprReader = new LineReader(ncprFileReader);
            int lineNum=0;
            while ((line = ncprReader.readLine()) != null) {
                lineNum++;
                if (StrUtil.isEmpty(line)) {
                    continue;
                }
                lineMap.put(NCPR_JSON+"-"+lineNum,line);
            }
        } catch (Throwable e) {
            throw new RuntimeException("json文件读取失败,"+JSON_PATH+NCPR_JSON);
        }finally {
            if (ncprFileReader!=null) {
                ncprFileReader.close();
            }
        }
        try{
            URL resources = ClassLoader.getSystemClassLoader().getResource(JSON_PATH+SKS_JSON);
            sksFileReader = new FileReader(resources.getFile());
            sksReader = new LineReader(sksFileReader);
            int lineNum=0;
            while ((line = sksReader.readLine()) != null) {
                lineNum++;
                if (StrUtil.isEmpty(line)) {
                    continue;
                }
                lineMap.put(SKS_JSON+"-"+lineNum,line);
            }
        } catch (Throwable e) {
            throw new RuntimeException("json文件读取失败,"+JSON_PATH+SKS_JSON);
        }finally {
            if (sksFileReader!=null) {
                sksFileReader.close();
            }
        }
        size=lineMap.size();
        keySet = lineMap.keySet();
        keyIterator = keySet.iterator();
    }

    /**
     * 如果有下一行将返回 {@code True}
     * <p>
     *  使用 {@link #getValue()} 获取json内容
     *  <p>
     *  使用 {@link #getKey()} 获取json标识(所属文件和行号)
     *
     */
    public boolean next(){
        if(keyIterator.hasNext()){
            currentKey = keyIterator.next();
            currentLine=lineMap.get(currentKey);
            return true;
        }else{
            currentKey=null;
            currentLine=null;
            return false;
        }
    }

    public String getKey(){
        return currentKey;
    }

    public String getValue(){
        return currentLine;
    }

    /**
     * 根据指定文件与行获取json
     * <p/>
     * 用于单个json测试
     */

    public String getValue(String key){
        return lineMap.get(key);
    }

}
